<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseAdminController;
use App\Lib\Common;
use App\Service\MemberService;
use App\Service\PostService;

class MemberController extends BaseAdminController{
    protected $memberService;
    protected $post;
    protected $org;
    protected $common;

    public function _init() {
        $this->memberService = new MemberService();
        $this->org = Common::getTree(getData('OrgDataTag'));
        $this->post = (new PostService)->selectSitePost();
        $this->common = new Common();
    }

    public function index(){
        $search = $this->request->input();
        $data = $this->memberService->getMemberList(isset($search['limit'])?$search['limit']:15,$search);
        $post = $this->post;
        return view("admin.member.index",[
            'data'=>$data,
            'post'=>$post,
            'org'=>json_encode($this->org)
        ]);
    }
    public function create(){
        if($this->request->isMethod("GET")){
            return view("admin.member.create",[
                "post"=>$this->post,
                "org"=>json_encode($this->org)
            ]);
        }else{
            $data = $this->request->except('_token');
            $this->validateRequest($this->request,"create");
            $salt = $this->common->get_salt();
            $data['salt'] = $salt;
            $data['password'] = $this->common->passWord($salt,$data['password']);
            $data['created_ip'] = $this->request->getClientIp();
            $data['site_id'] = getCurrentSiteId();
            $result = $this->memberService->create($data);
            if($result > 0){
                return $this->success($result,"添加成功");
            }else{
                return $this->error(7000,"添加失败");
            }
        }
    }
    public function update(){
        if($this->request->isMethod("GET")){
            $id = $this->request->input("id");
            $data = $this->memberService->find($id);
            return view("admin.member.update",[
                "data"=>$data,
                "post"=>$this->post,
                "org"=>json_encode($this->org)
            ]);
        }else{
            $data = $this->request->except('_token');
            $this->validateRequest($this->request,"update");
            $result = $this->memberService->update($data['id'],$data);
            if($result > 0){
                return $this->success($result,"编辑成功");
            }else{
                return $this->error(7000,"编辑失败");
            }
        }
    }
    public function profile(){
        if($this->request->isMethod("GET")){
            $id = $this->request->input('user_id');
            $data = $this->memberService->setModel("Admin.UserProfile")->find($id);
            return view("admin.member.profile",compact(['data']));
        }else{
            $data = $this->request->except('_token');
            $this->validateRequest($this->request,"profile");
            $result = $this->memberService->setModel("Admin.UserProfile")->update($data['id'],$data);
            if($result > 0){
                return $this->success($result,"操作成功");
            }else{
                return $this->error(7000,"操作失败");
            }
        }
    }
    public function destroy() {
        $id = $this->request->input('id');
        $result = $this->memberService->delete($id);
        if($result > 0){
            return $this->success($result, "删除成功");
        }else{
            return $this->error(7000,"操作失败");
        }
    }
    public function password(){
        if($this->request->isMethod('GET')){
            $id = $this->request->input('id');
            $data = $this->memberService->find($id);
            return view("admin.member.password",compact(['data']));
        }else{
            $data = $this->request->except("_token");
            if(!empty($data['password'])){
                if(strlen($data['password']) < 6){
                    return $this->error(7000,"密码必须大于6位");
                }
                $salt = $this->common->get_salt();
                $data['salt'] = $salt;
                $data['password'] = $this->common->passWord($salt,$data['password']);
                $result = $this->memberService->update($data['id'],['password'=>$data['password'],'salt'=>$salt]);
                if($result){
                    return $this->success($result,"操作成功");
                }else{
                    return $this->error(7000,"操作失败");
                }
            }else{
                return $this->success(7000,"操作成功");
            }

        }
    }
    public function isLocked(){
        $data = $this->request->input();
        $result = $this->memberService->update($data['id'],['is_locked'=>$data['is_locked']]);
        if($result){
            $res['code'] = 1;
            $res['msg'] = "操作成功";
        }else{
            $res['code'] = -1;
            $res['msg'] = "操作失败";
        }
        return $res;
    }
    public function upload(){
        $file = $this->request->file('img');
        return uploadImg($file,$this->request->post("path"));
    }
}
